//
//  ShareHelper.m
//  WanCaiC8
//
//  Created by tangang on 2018/5/4.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "ShareHelper.h"

@interface ShareHelper(){
   
}

@end

@implementation ShareHelper
static ShareHelper *helper=nil;
+ (instancetype)share{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper=[[ShareHelper alloc]init];
    });
    return helper;
}


+ (void)shareWithInvitationRegister:(NSInteger)type andDict:(NSDictionary *)dict{
    [ShareHelper share].url=[dict objectForKey:@"SuccessResLink"];
    if (type<2) {
        
        WXMediaMessage *message=[WXMediaMessage message];
        message.title=[dict objectForKey:@"Title"];
        message.description=[dict objectForKey:@"Describe"];
        [message setThumbData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dict objectForKey:@"Icon"]]]];
        
        WXWebpageObject *webObj=[WXWebpageObject object];
        webObj.webpageUrl=[dict objectForKey:@"Link"];
        message.mediaObject=webObj;
        
        SendMessageToWXReq *req=[[SendMessageToWXReq alloc]init];
        req.bText=NO;
        req.message=message;
        if (type==0) {
            req.scene =WXSceneSession;
        }else{
            req.scene =WXSceneTimeline;
        }
        
        [WXApi sendReq:req];
    }else{
        NSString *utf8String = [dict objectForKey:@"Link"];
        NSString *title = [dict objectForKey:@"Title"];
        NSString *description = [dict objectForKey:@"Describe"];
        NSString *previewImageUrl = [dict objectForKey:@"Icon"];
        QQApiNewsObject *newsObj = [QQApiNewsObject
                                    objectWithURL:[NSURL URLWithString:utf8String]
                                    title:title
                                    description:description
                                    previewImageURL:[NSURL URLWithString:previewImageUrl]];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
        //将内容分享到qq
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        if (sent==EQQAPISENDSUCESS) {
            
        }else{
            
        }
    }
    
    
    
}

+ (void)shareWithInfomation:(NSInteger)type andTitle:(NewsContentModel *)content{
    [ShareHelper share].url=content.Share.SuccessResLink;
    if (type<2) {
        WXMediaMessage *message=[WXMediaMessage message];
        message.title=content.Share.Title;
        message.description=content.Share.Describe;
        [message setThumbData:[NSData dataWithContentsOfURL:[NSURL URLWithString:content.Share.Icon]]];
        WXWebpageObject *webObj=[WXWebpageObject object];
        webObj.webpageUrl=content.Share.Link;
        message.mediaObject=webObj;
        
        SendMessageToWXReq *req=[[SendMessageToWXReq alloc]init];
        req.bText=NO;
        req.message=message;
        if (type==0) {
            req.scene =WXSceneSession;
        }else{
            req.scene =WXSceneTimeline;
        }
        
        [WXApi sendReq:req];
    }else{
        NSString *utf8String = content.Share.Link;
        NSString *title = content.Share.Title;
        NSString *description = content.Share.Describe;
        NSString *previewImageUrl = content.Share.Icon;
        QQApiNewsObject *newsObj = [QQApiNewsObject
                                    objectWithURL:[NSURL URLWithString:utf8String]
                                    title:title
                                    description:description
                                    previewImageURL:[NSURL URLWithString:previewImageUrl]];
        SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
        //将内容分享到qq
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        if (sent==EQQAPISENDSUCESS) {
            
        }else{
            
        }
    }
}


- (void)shareDone{
    if (!self.url) {
        return;
    }
    [[BaseVM manage] GET_Field_Url:self.url Params:nil successBlock:nil failBlock:nil];
}

#pragma mark -- qq分享回调
#pragma mark -- QQApiInterfaceDelegate
// 处理来自QQ的请求，调用sendResp
- (void)onReq:(QQBaseReq *)req {
    
}

// 处理来自QQ的响应，调用sendReq
- (void)onResp:(QQBaseReq *)resp {
    switch (resp.type)
    {
        case ESENDMESSAGETOQQRESPTYPE:
        {
            SendMessageToQQResp* sendResp = (SendMessageToQQResp*)resp;
            if ([sendResp.result isEqualToString:@"0"]) {
                [[ShareHelper share] shareDone];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"成功" message:@"QQ分享成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
                
            }
            else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"失败" message:@"QQ分享失败" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alert show];
            }
            break;
        }
        default:
        {
            break;
        }
    }
}

- (void)isOnlineResponse:(NSDictionary *)response {
    
}

- (void)tencentDidLogin {
    
}

- (void)tencentDidNotLogin:(BOOL)cancelled {
    
}

- (void)tencentDidNotNetWork {
    
}

@end
